/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.jsrpc.bridge;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.StringTokenizer;

/**
 *
 * @author neo
 */
public class Protocal {

    private String[] parser(String str, String determinate) {
        short ina = 0;
        short inb = 0;
        short inc = 0;
        List<String> list = new ArrayList<String>();
        StringBuffer b = new StringBuffer();
        if (str.startsWith("{") && str.endsWith("}")) {
            int p0 = str.indexOf("{");
            int p1 = str.lastIndexOf("}");
            str = str.substring(p0+1, p1) + ";";
            //System.out.println("source pre["+ str +"]");
        } else {
            str = str + ";";
        }
        StringTokenizer stk = new StringTokenizer(str, ":,[]\"{};", true);
        while (stk.hasMoreTokens()) {
            String s = stk.nextToken().trim();
            //System.out.println(s);
            if (s.equals("\"")) {
                switch (inb) {
                    case 0:
                        inb++;
                        //System.out.println("double open");
                        break;
                    case 1:
                        inb--;
                        //System.out.println("double close");
                        break;
                }
                b.append(s);
            } else {
                if (s.equals(determinate)) {
                    if ((ina == 0) && (inb == 0) && (inc == 0)) {
                        list.add(b.toString());
                        b.delete(0, b.length());
                        //System.out.println("terminate.");
                        } else {
                        b.append(s);
                    }
                    //System.out.println("colon inb[" + inb + "] inc[" + inc + "]");
                    } else {
                    if (s.equals("{")) {
                        b.append(s);
                        ina++;
                        //System.out.println("open { ");
                        } else {
                        if (s.equals("}")) {
                            b.append(s);
                            ina--;
                            //System.out.println("close } ");
                            } else {
                            if (s.equals("[")) {
                                b.append(s);
                                inc++;
                            } else {
                                if (s.equals("]")) {
                                    b.append(s);
                                    inc--;
                                } else {
                                    if (s.equals(";")) {
                                        list.add(b.toString());
                                    } else {
                                        b.append(s);
                                    }
                                }

                            }
                        }
                    }
                }
            }
        }

//        for (int i = 0; i < list.size(); i++) {
//            String string = list.get(i);
//            System.out.println("-[" + string + "]");
//        }
        return list.toArray(new String[list.size()]);
    }
    
    public Hashtable<String,String> getTable(String s) {
        String ss[] = parser(s, ",");
//        for (int i = 0; i < ss.length; i++) {
//            String string = ss[i];
//            System.out.println("Key1["+ string +"]");
//        }
        return getTable(ss);
    }

    private Hashtable getTable(String s[]) {
        Hashtable<String,String> table = new Hashtable<String,String>();
        //System.out.println("s size:" + s.length);
        for (int i = 0; i < s.length; i++) {
            System.out.println("s[" + s[i] + "]");
            //if( s[i].length() == 2 ) { 
            String string[] = parser(s[i], ":");
            //System.out.println("s size["+ string.length +"]");
            String key = string[0];
            String value = string[1];
            if (string[0].startsWith("\"") && string[0].endsWith("\"")) {
                key = string[0].substring(1, string[0].length() - 1);
            }
//            if (string[1].startsWith("\"") && string[1].endsWith("\"")) {
//                value = string[1].substring(0, string[1].length() - 1);
//            }
            if (string[0].startsWith("[") && string[0].endsWith("]")) {
                key = string[0].substring(1, string[0].length() - 1);
            }
            if (string[1].startsWith("[") && string[1].endsWith("]")) {
                value = string[1].substring(1, string[1].length() - 1);
            }
            //System.out.println("key[" + key + "]value[" + value + "]");
            table.put(key, value);
           // }
        }
        return table;
    }
}
